//
//  AppDelegate.m
//  Weibo
//
//  Created by student on 16/4/21.
//  Copyright © 2016年 Rihanna. All rights reserved.
//

#import "AppDelegate.h"
#import "ProfileViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "HomeViewController.h"


@interface AppDelegate ()

@property (nonatomic,strong)UITabBarController *tabBarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //1.创建窗体
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //创建视图控制器
//
//    self.window.rootViewController = vc;

  
    //2.创建TabBarController
  self.tabBarController = [[UITabBarController alloc] init];
    
    self.tabBarController.tabBar.tintColor = [UIColor orangeColor];
    
    [self addChildViewController:[[HomeViewController alloc] init] title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewController:[[MessageViewController alloc] init] title:@"消息" imageName:@"tabbar_message"];
    [self addChildViewController:[[UITableViewController alloc] init] title:@"" imageName:@""];
    [self addChildViewController:[[DiscoverViewController alloc] init] title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewController:[[HomeViewController alloc] init] title:@"我" imageName:@"tabbar_profile"];
    
    //创建视图控制器
   
    self.window.rootViewController = self.tabBarController;
    
    //5添加中间按钮
    [self addComposeBtn];
    //让窗体可见并激活
    [self.window makeKeyAndVisible];
    return YES;
}

//封装一下
- (void)addChildViewController:(UIViewController *)vc
                         title:(NSString *)title
                       imageName:(NSString *)imageName

{

    //1.设置其tabbarItem上的图标
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]];
    //2.设置tabbar上的文字
//  vc.tabBarItem.title = title;
    
    //3.嵌入到一个导航栏控制器中
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    //4.设置导航上的标题
//    vc.navigationItem.title = title;
    //注:2和4可以用以下一行替代
    vc.title = title;
    //5.将导航栏控制器添加到tabBar
    [self.tabBarController addChildViewController:navi];

}

//添加中间的按钮
- (void)addComposeBtn
{
    UIButton *btn = [[UIButton alloc] init];
    //图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    //背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    //设置尺寸图片
    int width = [UIScreen mainScreen].bounds.size.width / 5;
    btn.frame = CGRectMake(width * 2, 0 , width, 49);
    //添加按钮事件
    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    //添加到tabbar上
    [self.tabBarController.tabBar addSubview:btn];
    
}

- (void)composeBtnPressed
{
    NSLog(@"%s",__func__);
}

@end
